using System;
using System.Collections.Generic;
using System.Text;
using Forecast.Database;

namespace Forecast.Business
{
    public class ActivatePrediction
    {
        #region Variables

        private Prediction _prediction;
        private IList<Player> _players;
        private IList<Holding> _holdings;
        private NewsItem _newsItem;

        #endregion
        #region Interface

        public void Execute(Prediction prediction, IList<Player> allPlayers)
        {
            if (prediction == null)
                throw new ArgumentNullException("prediction");
            if (allPlayers == null)
                throw new ArgumentNullException("allPlayers");
            foreach (Player player in allPlayers)
            {
                if (player == null)
                    throw new ArgumentNullException("player");
            }

            if (prediction.HasSucceeded)
                throw new ArgumentException("Prediction " + prediction.Ticker + " has already succeeded.");
            if (prediction.IsClosed)
                throw new ArgumentException("Prediction " + prediction.Ticker + " is already closed.");
            if (prediction.IsActive)
                throw new ArgumentException("Prediction " + prediction.Ticker + " has already been activated.");
            if (prediction.EndDate < DateTime.Now)
                throw new ArgumentException("Prediction " + prediction.Ticker + " end date was " + prediction.EndDate + ".");
            
            _prediction = prediction;
            _players = allPlayers;
            PerformActivation();
        }

        #endregion
        #region Private methods

        private void PerformActivation()
        {
            _prediction.IsActive = true;

            _holdings = new List<Holding>();
            foreach (Player player in _players)
            {
                player.Cash += 500;

                Holding holding = new Holding();
                holding.NumPredictions = 50;
                holding.PlayerId = player.Id;
                holding.PredictionId = _prediction.Id;
                _holdings.Add(holding);
            }

            _newsItem = new NewsItem();
            _newsItem.Message = _prediction.Ticker + " has been activated. "
                + "All traders received 50 stocks in the prediction and 500 cash each."
                + " Trading is allowed until " + _prediction.EndDate + "."
                + " Ladies & Gentlemen, start your trading!";
            _newsItem.NewsDate = DateTime.Now;
            _newsItem.PredictionId = _prediction.Id;
            _newsItem.Title = _prediction.Ticker + " activated.";
            _newsItem.ShouldBeShownInMainNews = true;
        }

        #endregion
        #region Properties

        public NewsItem ActivatePredictionNewsItem
        {
            get { return _newsItem; }
        }

        public Prediction ActivatePredictionPrediction
        {
            get { return _prediction; }
        }

        public IList<Player> ActivatePredictionPlayers
        {
            get { return _players; }
        }

        public IList<Holding> ActivatePredictionHoldings
        {
            get { return _holdings; }
        }

        #endregion
    }
}
